/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package project.ejbs;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import project.entity.ListaAutor;
import project.entity.Revisao;
import project.entity.Revisor;
import project.entity.Submissao;

/**
 *
 * @author serafim
 */
@Stateless
public class RevisorFacade extends AbstractFacade<Revisor> {
    @PersistenceContext(unitName = "DAEPU")
    private EntityManager em;

    protected EntityManager getEntityManager() {
        return em;
    }

    public RevisorFacade() {
        super(Revisor.class);
    }
    
    public int countUsername(String username, Submissao submissao) {
        Query cq = getEntityManager().createNamedQuery("searchRevUsername");
        cq.setParameter("username", username);
        List lb = cq.getResultList();
        verificaRevisores(lb, submissao);
        return lb.size();
    }
    
    public int countPNome(String pNome, Submissao submissao) {
        Query cq = getEntityManager().createNamedQuery("searchRevPNome");
        cq.setParameter("pNome", pNome);
        List lb = cq.getResultList();
        verificaRevisores(lb, submissao);
        return lb.size();
    }
    
    public int countUNome(String uNome, Submissao submissao) {
        Query cq = getEntityManager().createNamedQuery("searchRevUNome");
        cq.setParameter("uNome", uNome);
        List lb = cq.getResultList();
        verificaRevisores(lb, submissao);
        return lb.size();
    }
    
    public int countEmail(String email, Submissao submissao) {
        Query cq = getEntityManager().createNamedQuery("searchRevEmail");
        cq.setParameter("email", email);
        List lb = cq.getResultList();
        verificaRevisores(lb, submissao);
        return lb.size();
    }
    
    public int countEmpresa(String empresa, Submissao submissao) {
        Query cq = getEntityManager().createNamedQuery("searchRevEmpresa");
        cq.setParameter("empresa", empresa);
        List lb = cq.getResultList();
        verificaRevisores(lb, submissao);
        return lb.size();
    }
    
    public List<Revisor> searchUsername(int[] range, String username, Submissao submissao) {
        Query cq = getEntityManager().createNamedQuery("searchRevUsername");
        cq.setParameter("username", username);
        cq.setMaxResults(range[1] - range[0]);
        cq.setFirstResult(range[0]);
        List lb = cq.getResultList();
        verificaRevisores(lb, submissao);
        return lb;
    }
    
    public List<Revisor> searchPNome(int[] range, String pNome, Submissao submissao) {
        Query cq = getEntityManager().createNamedQuery("searchRevPNome");
        cq.setParameter("pNome", pNome);
        cq.setMaxResults(range[1] - range[0]);
        cq.setFirstResult(range[0]);
        List lb = cq.getResultList();
        verificaRevisores(lb, submissao);
        return lb;
    }
    
    public List<Revisor> searchUNome(int[] range, String uNome, Submissao submissao) {
        Query cq = getEntityManager().createNamedQuery("searchRevUNome");
        cq.setParameter("uNome", uNome);
        cq.setMaxResults(range[1] - range[0]);
        cq.setFirstResult(range[0]);
        List lb = cq.getResultList();
        verificaRevisores(lb, submissao);
        return lb;
    }
    
    public List<Revisor> searchEmail(int[] range, String email, Submissao submissao) {
        Query cq = getEntityManager().createNamedQuery("searchRevEmail");
        cq.setParameter("email", email);
        cq.setMaxResults(range[1] - range[0]);
        cq.setFirstResult(range[0]);
        List lb = cq.getResultList();
        verificaRevisores(lb, submissao);
        return lb;
    }
    
    public List<Revisor> searchEmpresa(int[] range, String empresa, Submissao submissao) {
        Query cq = getEntityManager().createNamedQuery("searchRevEmpresa");
        cq.setParameter("empresa", empresa);
        cq.setMaxResults(range[1] - range[0]);
        cq.setFirstResult(range[0]);
        List lb = cq.getResultList();
        verificaRevisores(lb, submissao);
        return lb;
    }
    
    private void verificaRevisores(List lb, Submissao actual) {
        ArrayList<Integer> idx = new ArrayList<Integer>();
        boolean flag = true;
        for (Iterator it = lb.iterator(); it.hasNext();) {
            Revisor obj = (Revisor) it.next();
            for (Iterator it1 = obj.getRevisoes().iterator(); it1.hasNext() && flag;){
                Revisao obj2 = (Revisao) it1.next();
                if (obj2.getSubmissao().equals(actual)){
                    idx.add(lb.indexOf(obj));
                    flag = false;
                }
            }
            for (Iterator<ListaAutor> it2 = actual.getListaAutores().iterator(); it2.hasNext() && flag;) {
                ListaAutor listaAutor = it2.next();
                if (listaAutor.getEmail().equals(obj.getEmail())){
                    idx.add(lb.indexOf(obj));
                    flag = false;
                }
            }
            flag = true;
        }
        for (int i = idx.size() - 1; i >= 0; i--) {
            lb.remove(idx.get(i).intValue());
        }
    }
}
